<script type="text/javascript">
  function selectAll()
	{
		var inputs = document.getElementsByTagName('input');
		for(id = 0; id < inputs.length; id++)
		{
      if(inputs[id].type == 'checkbox' && inputs[id].name.substring(4,0) == 'msg_')
	      inputs[id].checked = $('select_all_msgs').checked;
    }
  }
	
	function isSmthSelected()
	{
		var inputs = document.getElementsByTagName('input');
		for(id = 0; id < inputs.length; id++)
		{
      if(inputs[id].type == 'checkbox' && inputs[id].name.substring(4,0) == 'msg_' && inputs[id].checked)
	      return( true );
    }
	}
	
	function getSelectedMsgIDs()
	{
		var inputs = document.getElementsByTagName('input');
		var res = "";
		for(id = 0; id < inputs.length; id++)
		{
      if (inputs[id].type == 'checkbox' && inputs[id].name.substring(4, 0) == 'msg_' && inputs[id].checked)
			  res = res + inputs[id].name + ";"; 
    }
		
		return( res );
	}
</script>


<% # determine which mode this partial currently works in: inbox or outbox; -%>
<% # certain feature will only be visible in the inbox -%>
<% inbox_mode = (message_folder == "inbox") -%>

<% unless collection.empty? %>

	<table class="alt_table" id="msg_display_table" name="msg_display_table" >
	  <tr>
	    <th><%= check_box_tag( "select_all_msgs", "1", false, :onclick => "selectAll();" ) -%></th>
			<% if inbox_mode -%>
			  <th><%= link_to "Status" + current_ordering_icon("status"), messages_path + "?sort_by=status&order=" + next_ordering("status"), :title => tooltip_title_attrib(tooltip_message("status", opposite_ordering(next_ordering("status")))) -%></th>
			
			<th>From</th>
			<% else -%>
			<th>To</th>
				<% end -%>
	    <th><%= link_to "Subject" + current_ordering_icon("subject"), (inbox_mode ? messages_path : sent_messages_path) + "?sort_by=subject&order=" + next_ordering("subject"), :title => tooltip_title_attrib(tooltip_message("subject", opposite_ordering(next_ordering("subject")))) -%></th>
	    <th><%= link_to (inbox_mode ? "Received at" : "Sent at") + current_ordering_icon("date"), (inbox_mode ? messages_path : sent_messages_path) + "?sort_by=date&order=" + next_ordering("date"), :title => tooltip_title_attrib(tooltip_message("date", opposite_ordering(next_ordering("date")))) -%></th>
	    <th> </th>

	  </tr>
	  
	<% odd_row = false -%>
	<% for message in collection %>
	  <tr class="<%= (odd_row = !odd_row) ? "odd_row" : "even_row" %>">
	    <td style="text-align: center; vertical-align: top; width: 15px; font-size: 85%;">
			  <%= check_box_tag( "msg_" + message.id.to_s ) -%>
			</td>
			<% if inbox_mode -%>
				<td style="width: 80px; text-align: center;">
					<%= icon(message.read? ? 'message_read' : 'message', nil, (message.read? ? 'Read Message' : 'Unread Message'), nil, '') %>
				</td>
				<td style="text-align: center; width: 45px; font-size: 85%;">
				  <%= "<b>" unless (message.read?) %><%= render :partial => "users/avatar", :locals => { :user => message.from, :size => 40 } %><%= "</b>" unless (message.read?) %>
			  </td>
			<% else -%>
			<td style="text-align: center; width: 45px; font-size: 85%;">
			  <%= "<b>" unless (message.read?) %><%= render :partial => "users/avatar", :locals => { :user => message.to, :size => 40 } %><%= "</b>" unless (message.read?) %>
		  </td>
	    <% end -%>
			
	    <td style="text-align: center;">
			  <%= "<b>" unless (message.read? || !inbox_mode) %><%=link_to h(message.subject), message_path(message) %><%= "</b>" unless (message.read? || !inbox_mode) %>
		  </td>
	    <td style="width: 185px">
	    	<%= "<b>" unless (message.read? || !inbox_mode) %><%=message.created_at -%><%= "</b>" unless (message.read? || !inbox_mode) %>
			</td>
	    <td class="actions" style="width: 100px">
	        <%= icon('show', message_path(message), nil, nil, (inbox_mode ? 'Read' : 'View')) %>
	        <% if inbox_mode -%>
						<%= icon('reply', url_for(:controller => :messages, :action => :new, :reply_id => message.id), nil, nil, 'Reply') %>
	        <% end -%>

			  <%= icon "destroy", message_path(message), "Delete Message", { :confirm => 'Are you sure?', :method => :delete }, "Delete" -%>
			</td>
	  </tr>
	<% end %>
	</table>
	
	<br>
	<a class="button_slim" href="/messages/delete_all_selected" onclick="if (isSmthSelected()) { if(confirm('Are you sure you want to delete all selected messages?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m); var n = document.createElement('input'); n.setAttribute('type', 'hidden'); n.setAttribute('name', 'msg_ids'); n.setAttribute('value', getSelectedMsgIDs()); f.appendChild(n); var o = document.createElement('input'); o.setAttribute('type', 'hidden'); o.setAttribute('name', 'deleted_from'); o.setAttribute('value', '<%=message_folder-%>'); f.appendChild(o); f.submit(); }} else{ alert('Please tick checkboxes next to the messages that you want to delete'); };return false;"><%= delete_image("vertical-align: middle", "") -%><span style="vertical-align: middle;"> Delete all selected messages</span></a>		


<% else %>

	<p class="none_text" style="margin-left: 1.5em">No Messages</p>

<% end %>